package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import javax.swing.*;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class ProductCollectionPage extends Utils {
    public static String url = "http://49.235.61.184/?s=usergoodsfavor/index.html";

    public ProductCollectionPage() {
        super(url);
    }
    /**
     * 检查页面元素
     */
    public void checkProductCollectionPage() throws IOException {
        //检查页面标题
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.am-flex.am-flex-items-center.am-margin-bottom-main > h1"));
        //检查表头
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.am-form-table-data-list-container.am-scrollable-horizontal.am-table-scrollable-horizontal.am-radius > table > thead > tr"));
        //检查”删除“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > button"));
        //检查操作中的”详情”按钮
        driver.findElement(By.cssSelector("button.submit-popup.am-btn-default"));
        //检查操作中的“删除”按钮
        driver.findElement(By.cssSelector("button.submit-delete.am-btn-danger"));
        //检查分页
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul"));
        //检查每页第三条的输入框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.current-page-input > input"));
        //检查跳转到多少页的输入框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.to-page-input > input"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

    }
    //测试一：搜索商品
    public void collectionCheckProduct() throws IOException, InterruptedException {
        //输入想要搜索的商品信息
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.am-form-table-data-list-container.am-scrollable-horizontal.am-table-scrollable-horizontal.am-radius > table > thead > tr > th.form-table-search-item-head-title-goods.form-table-search-item-head-value-goods.am-text-top.am-grid-lg > div > input")).sendKeys("手机");
        //输入销售价格最小值
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[3]/div/div[2]/input[1]")).sendKeys("3000");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[3]/div/div[2]/input[2]")).sendKeys("6000");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[4]/div/div[2]/input[1]")).sendKeys("6000");
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[4]/div/div[2]/input[2]")).sendKeys("7000");
        //点击”搜索图片“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > button.am-btn.am-radius.btn-loading-example.form-table-operate-top-search-submit")).click();
       Thread.sleep(2000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //进行重置
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > a")).click();
    }

    /**
     * 测试二：删除商品
     */

    public void collectionDeleteProduct() throws IOException, InterruptedException {
        //删除单个商品
        String str = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > div > span:nth-child(1)")).getText();
        //点击操作中“删除”按钮
        driver.findElement(By.cssSelector("button.submit-delete.am-btn-danger")).click();
        //弹出提示框，点击“确认”按钮
        //标签名 + 类名 + 属性选择器
        driver.findElement(By.cssSelector("span.am-modal-btn[data-am-modal-confirm]")).click();
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //刷新，获取一共有多少条数据
        driver.navigate().refresh();
        String str1 = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > div > span:nth-child(1)")).getText();
        assert !str1.equals(str);


        //删除一个或者多个商品
        //勾选商品信息前的方框
        driver.findElement(By.cssSelector("table tbody tr:nth-child(1) td:first-child i.am-icon-checked")).click();
        driver.findElement(By.cssSelector("table tbody tr:nth-child(3) td:first-child i.am-icon-checked")).click();
        Thread.sleep(2000);
        //点击“删除”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > button")).click();
        //弹出提示框，点击“确认”按钮
        driver.findElement(By.cssSelector("span.am-modal-btn[data-am-modal-confirm]")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //刷新，获取一共有多少条数据
        driver.navigate().refresh();
        String str2 = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > div > span:nth-child(1)")).getText();
        assert !str2.equals(str1);
    }

    /**
     *测试三：分页、跳转页
     */
    public void testPaging() throws IOException, InterruptedException {
        //定位输入框元素
        WebElement webElement = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.current-page-input > input"));
       //双击选中输入框所有文本 → 按DELETE删除 → 执行操作
        new Actions(driver).doubleClick(webElement).sendKeys(Keys.DELETE).perform();
        //输入新值+回车
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.current-page-input > input")).sendKeys("2",Keys.ENTER);
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //设置跳转第几个页面
        webElement = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.to-page-input > input"));
        new Actions(driver).doubleClick(webElement).sendKeys(Keys.DELETE).perform();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.to-page-input > input")).sendKeys("3",Keys.ENTER);
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(3000);
        //页面跳转
        //点击”<<“按钮 - 首页
        driver.findElement(By.cssSelector("li.first-before-page-submit a")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //点击”>>“按钮 - 尾页
        driver.findElement(By.cssSelector("li.last-after-page-submit a")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //点击”<“按钮 - 上一页
        driver.findElement(By.cssSelector("li.prev-before-page-submit a")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //点击”>“按钮 - 下一页
        driver.findElement(By.cssSelector("li.next-after-page-submit a")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


    }

}
